package edu.hqu.rent_system.controller;

import com.alibaba.fastjson.JSON;
import edu.hqu.rent_system.common.Result;
import edu.hqu.rent_system.config.passwordCoding;
import edu.hqu.rent_system.pojo.House;
import edu.hqu.rent_system.pojo.Meeting;
import edu.hqu.rent_system.pojo.Record;
import edu.hqu.rent_system.pojo.Tenant;
import edu.hqu.rent_system.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
public class TenantController {

    @Autowired
    private TenantService tenantService;

    //租客查询租房记录
    @RequestMapping("/queryTenantRecord")
    public Result queryTenantRecord(@RequestParam("tId") int tId) {
        try {
            List<Record> records = tenantService.queryTenantRecord(tId);
            if (records != null || records.size() != 0){
                return Result.success(records);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    //租客修改个人信息
    @RequestMapping("/updateTenantInform")
    public Result updateTenantInform(@RequestParam("tenantStr") String tenantStr) {
        try {
            Tenant tenant = JSON.parseObject(tenantStr, Tenant.class);
            tenant.setPassword(passwordCoding.getHash2(tenant.getPassword(),"MD5"));
            int i = tenantService.updateTenantInform(tenant);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    //租客查询所有可租房屋
    @RequestMapping("/queryAllHouse")
    public Result queryAllHouse(){
        try {
            List<House> houses = tenantService.queryAllHouse();
            if (houses != null || houses.size() != 0){
                return Result.success(houses);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    //租客模糊查询所有可租房屋
    @RequestMapping("/queryHouseLike")
    public Result queryHouseLike(@RequestParam("inputInform") String inputInform) {
        try {
            List<House> houses = tenantService.queryHouseLike(inputInform);
            if (houses != null || houses.size() != 0){
                return Result.success(houses);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    //租客发出租房请求
    @RequestMapping("/addMeetingByTenant")
    public Result addMeetingByTenant(@RequestParam("meetingStr") String meetingStr) {
        try {
            Meeting meeting = JSON.parseObject(meetingStr, Meeting.class);
            //查询所有会面信息，如果已经有了，则不能重复申请
            List<Meeting> meetings = tenantService.queryMeetingByTenant(meeting.getTId());
            for (Meeting m : meetings) {
                if (m.getHId() == meeting.getHId()) {
                    return Result.failByCusMsg("已申请过该房屋",444);
                }
                else meeting.setStatus("未确认");
            }
            meeting.setStatus("未确认");
            int i = tenantService.addMeetingByTenant(meeting);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failByCusMsg("申请失败",445);
        }
    }

    //租客查询会面信息
    @RequestMapping("/queryMeetingByTenant")
    public Result queryMeetingByTenant(@RequestParam("tId") int tId) {
        try {
            List<Meeting> meetings = tenantService.queryMeetingByTenant(tId);
            if (meetings != null || meetings.size() != 0){
                return Result.success(meetings);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    //租客删除会面信息
    @RequestMapping("/deleteMeetingByTenant")
    public Result deleteMeetingByTenant(@RequestParam("mId") int mId) {
        try {
            // 先判断是否确认，如果已确认，则不能删除
            String s = tenantService.queryMeetingStatusByMId(mId);
            if ("未确认".equals(s)){
                int i = tenantService.deleteMeetingByTenant(mId);
                return Result.success(i);
            }
            else return Result.failByCusMsg("无法删除",444);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    //查询租客信息
    @RequestMapping("/queryTenantInform")
    public Result queryTenantInform(@RequestParam("tId") int tId) {
        try {
            Tenant tenant = tenantService.queryTenantInform(tId);
            if (tenant != null) {
                return Result.success(tenant);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
